<template>
  <div>
    <pageHeader :title="title" :breadCrumb="breadCrumb" :show-back="true">
    </pageHeader>
    <div style="margin-bottom: 150px">
      <div class="info-item">
        <TitleBox title="基本消息"></TitleBox>
        <div style="margin-left: 10%"
          ><a-form :model="taskData" auto-label-width>
            <a-row :gutter="24">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="关联采购入库单号:"
                  ><a-input
                    disabled
                    style="width: 320px"
                    v-model="form.entryNumber"
                /></a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="物料编码：">
                  <a-input
                    disabled
                    style="width: 320px"
                    v-model="form.productNumber"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="物料名称:"
                  ><a-input
                    disabled
                    style="width: 320px"
                    v-model="form.productName"
                    placeholder=""
                    allow-clear
                /></a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="规格型号："
                  ><a-input
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    v-model="form.spec"
                    disabled
                    allow-clear
                /></a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="已入库数量：">
                  <a-input
                    :style="{ width: '320px' }"
                    v-model="form.amount"
                    disabled
                    allow-clear
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="已上架数量：">
                  <a-input
                    v-model="form.receivedAmount"
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    disabled
                    allow-clear
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="暂存区域：">
                  <a-typography-text v-if="form.state === 3" type="primary">
                    上架中
                  </a-typography-text>
                  <a-typography-text v-if="form.state === 4" type="success">
                    上架完成
                  </a-typography-text>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div style="width: 100%; display: flex; margin: 20px 50px">
          <a-space>
            <div
              style="
                width: 4px;
                height: 15px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            >
            </div>
            <h3>质检与入库信息</h3>
          </a-space>
        </div>
        <div style="margin-left: 10%"
          ><a-form :model="taskData.form" auto-label-width>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="质检员：">
                  <a-space>
                    <a-tooltip
                      content="注：可以直接选择质检员下发质检任务，如不指定则直接进入“来料质检任务”待检任务中。"
                      position="top"
                    >
                      <icon-exclamation-circle-fill
                        style="font-size: 20px; color: blue"
                      />
                    </a-tooltip>

                    <a-input
                      v-model="form.inspectionStaffName"
                      :style="{ width: '320px' }"
                      placeholder="Please enter something"
                      disabled
                      allow-clear
                    />
                  </a-space>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="仓管员：">
                  <a-input
                    v-model="form.warehouseStaffName"
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    disabled
                    allow-clear
                /></a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div style="width: 100%; display: flex; margin: 20px 50px">
          <a-space>
            <div
              style="
                width: 4px;
                height: 15px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            >
            </div>
            <h3>采购明细：</h3>
          </a-space>
        </div>
        <div
          style="
            width: 90%;
            margin-left: 5%;
            box-shadow: 1px 1px 5px rgb(153 153 153 / 35%);
            border-radius: 4px;
          "
        >
          <div style="padding: 30px">
            <div style="text-align: right" class="button-item"></div
            ><tableTurn
              style="margin-top: 10px"
              :table-data="getProduct"
              ref="tableRef"
              :pagination="false"
              :args="args"
            >
              <a-table-column align="center" :width="80" title="条码">
                <template #cell="{ record }">
                  <div style="padding: 10px">
                    <QRcode
                      ref="qrcode"
                      :qr-url="`${record.content}`"
                      :qr-text="`${record.content}`"
                    /> </div
                ></template>
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="条码编号"
                data-index="content"
              >
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="总需上架量"
                data-index="amount"
              >
              </a-table-column>

              <a-table-column
                align="center"
                :width="80"
                title="状态"
                data-index="locationName"
              >
                <template #cell="{ record }">
                  <a-typography-text
                    v-if="record.entryState !== 4"
                    type="primary"
                  >
                    待上架
                  </a-typography-text>
                  <a-typography-text v-else type="success">
                    已上架
                  </a-typography-text>
                </template>
              </a-table-column>
            </tableTurn></div
          >
        </div>
      </div>
    </div>
  </div>
  <SelectedPosition
    v-if="modelVisible"
    v-model:visible="modelVisible"
    :productName="productName"
    @selectLocationEdit="selectLocationEdit"
  ></SelectedPosition>
  <selectedEntryAmount v-model:visible="modelVisibleS"></selectedEntryAmount>
</template>

<script lang="ts" setup>
  import {
    reactive,
    ref,
    watchEffect,
    nextTick,
    defineAsyncComponent,
  } from 'vue';
  import { useRoute } from 'vue-router';
  import {
    getPurchaseOrderList,
    getSinglePurchaseOrder,
  } from '@/api/buy-management/buy-order/index';
  import {
    GetPersonnelinformation,
    Getsingleperson,
  } from '@/api/home/System settings/personal-information/index';
  import { getWarehouseEntrySingle } from '@/api/inventory-management/warehousing-management/receiving-management/index';
  import {
    postWarehouseGrounding,
    getWarehouseGrounding,
  } from '@/api/inventory-management/warehousing-management/purchase-receipt/index';
  import { getBarcode, getProductAndBarcode } from '@/api/barcode';
  import { cloneDeep } from 'lodash';
  // import SelectedPosition from './selected-position/index.vue';
  let SelectedPosition = defineAsyncComponent(
    () => import('./selected-position/index.vue')
  );
  let selectedEntryAmount = defineAsyncComponent(
    () => import('./selected-entry-amount/index.vue')
  );

  const route = useRoute();
  const id = Number(route.query.id);
  const productName = ref();
  const modelVisible = ref(false);
  const modelVisibleS = ref(false);
  // 获取人员信息
  const Personne = ref();
  const Personnelinformation = async () => {
    const res = await GetPersonnelinformation({ page: 0, size: -1 });
    Personne.value = res.content.data;
  };
  Personnelinformation();

  const tableRef = ref();
  const title: string = '物料上架详情';
  const breadCrumb: string[] = [
    '库存管理',
    '入库管理',
    '委外入库',
    '物料上架详情',
  ];

  const form = ref<any>({
    storageWarehouseItemDetails: [{ name: '22', id: 1 }],
  });

  // const getQrcodeList = ref();
  // const getQrcodeData = async () => {
  //   const res = await getProductAndBarcode({
  //     orderNumber: form.value.purchaseOrderNumber,
  //     state: 2,
  //   });
  //   getQrcodeList.value = res;
  // };

  const getData = async () => {
    const res = await getWarehouseGrounding({ id });
    form.value = res;
    tableRef.value.loadData();
    // getQrcodeData();
  };
  getData();

  // 表格配置
  const args = reactive({
    stripe: false,
    scroll: {
      y: 2000,
      x: 500,
    },
    bordered: {
      headerCell: true,
    },
    rowKey: 'id', // 批量选择按钮 以合同名称来作为key
  });

  let taskData = reactive<any>({
    form: {},
  });

  const getProduct = async () => {
    try {
      return Promise.resolve(form.value.barcodes);
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const BuyOrderList = ref();
  const getBuyOrderList = async () => {
    const res = await getPurchaseOrderList({ page: 1, size: -1 });
    BuyOrderList.value = res.content;
  };
  getBuyOrderList();
  const isSelectSupplier = ref(false);

  const getUserInfo = async (id: any) => {
    const res = await Getsingleperson({ id });
    form.value.StaffPhone = res.mobile;
  };

  // const addDetails = (record: any) => {
  //   // record.d;
  //   record.entryWarehouseItemDetails.push({
  //     itemId: record.id,
  //     amount: undefined,
  //     locationId: undefined,
  //     state: undefined,
  //   });
  // };

  const delDetails = (record: any, index: number) => {
    record.entryWarehouseItemDetails.splice(index, 1);
  };

  // const fatherIndex = ref();
  // const sonIndex = ref();
  // const selectedLocation = (rowIndex: number, index: number, val: any) => {
  //   productName.value = val.productName;
  //   fatherIndex.value = rowIndex;
  //   sonIndex.value = index;
  //   modelVisible.value = true;
  // };

  // const selectLocationEdit = (data: any) => {
  //   console.log(data);
  //   form.value.purchaseOrderProduct[
  //     fatherIndex.value
  //   ].entryWarehouseItemDetails[sonIndex.value].locationId = data.locationId;
  //   form.value.purchaseOrderProduct[
  //     fatherIndex.value
  //   ].entryWarehouseItemDetails[sonIndex.value].locationName =
  //     data.locationName;
  //   console.log(data, form.value, fatherIndex.value, sonIndex.value);
  //   tableRef.value.loadData();
  // };

  // const ConfirmListing = async () => {
  //   console.log(form.value.purchaseOrderProduct[0].entryWarehouseItemDetails);
  //   // let details = form.value.purchaseOrderProduct;
  //   await postWarehouseGrounding({
  //     details: form.value.purchaseOrderProduct[0].entryWarehouseItemDetails,
  //   });
  // };

  // nextTick(() => {
  //   if (form.value.storageWarehouseItemDetails) {
  //     tableRef.value.loadData();
  //   }
  // });
</script>

<style scoped lang="less">
  .basic-item {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid rgba(242, 243, 245, 0.852);
  }

  .title {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .title-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
    margin-top: 20px;
  }

  .info-item {
    // width: 100%;
    padding: 10px;
  }

  .report-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
  }

  .order-info {
    width: 95%;
    margin: 20px auto;
  }

  :deep(.arco-form) {
    width: 90%;
  }

  :deep(.arco-input-wrapper),
  :deep(.arco-input[disabled]) {
    -webkit-text-fill-color: rgb(0, 0, 0);
  }

  :deep(.arco-picker-disabled),
  :deep(.input[disabled]) {
    -webkit-text-fill-color: rgb(0, 0, 0);
  }
  :deep(.arco-table),
  :deep(.arco-table-cell) {
    padding: 0;
  }
</style>
